package com.atguigu.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest004pool {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws Exception{
        System.out.println("main...........start........");


/*        方法串行化
        不能获取上一步的执行结果
        .thenRunAsync(()->{
            System.out.println("任务二启动了");
        },executorService);

        2接受上一步的结果，无返回值
        .thenAcceptAsync(res->{
            System.out.println("任务二启动了....."+res);
        },executorService);
        3


        */

/*
        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一线程：" + Thread.currentThread().getId());
            int i = 10 % 4;
            System.out.println("任务一结束：" + i);
            return i;
        }, executorService);

        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二线程：" + Thread.currentThread().getId());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务二结束：" );
            return "hello";
        }, executorService);

*/

        //不能感知结果
//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务三开始。。。");
//        },executorService);

//        future01.thenAcceptBothAsync(future02,(f1,f2)->{
//            System.out.println("任务3开始。。。之前的结果："+f1+"&&"+f2);
//        },executorService);

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ":" + f2 + "->haha";
//        }, executorService);

        /**
         * 两个任务，只要有一个完成，我们就执行任务3
         * 不感知结果，无返回值
         */
//        future01.runAfterEitherAsync(future02,()->{
//            System.out.println("任务三开始。。。");
//        },executorService);

        //感知结果，无返回值
//        future01.acceptEitherAsync(future02,(res)->{
//            System.out.println("任务三开始。。。"+res);
//        },executorService);

        //感知结果，有返回值
//        CompletableFuture<String> future = future01.applyToEitherAsync(future02, (res) -> {
//            System.out.println("任务三开始。。。" + res);
//            return res.toString() + "-->aa";
//        }, executorService);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品 的图片信息");
            return "hello.jpg";
        },executorService);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品 的属性");
            return "黑色+256G";
        },executorService);
        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品 的介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        },executorService);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);

        anyOf.get();//等待所有结果完成

//        System.out.println("main...........end........"+futureImg.get()+"--2--"+futureAttr.get()+"--3--"+futureDesc.get());
        System.out.println("main...........end........"+anyOf.get());
    }
    public  void thread(String[] args) {
        System.out.println("main...........start........");
        Thread01 thread01 = new Thread01();
        thread01.start();
        System.out.println("main...........end........");
    }
    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程："+Thread.currentThread().getId());
            int i = 10%3 ;
            System.out.println("运行结果："+i);
        }
    }
}